//
//  STOrderNewCollectionViewCell.swift
//  SeaTransportManage
//
//  Created by guopengwen on 2021/11/26.
//

import UIKit

protocol STOrderNewActionDelegate: NSObjectProtocol  {
    func handleReturnAction(indexPath: IndexPath) ->Void
    func handlWaitAction(indexPath: IndexPath) ->Void
}

class STOrderNewCollectionViewCell: UICollectionViewCell {
    
    class var identifier: String {
        get {
            return "STOrderNewCollectionViewCell"
        }
    }
    
    class func registerTo(_ collectionView: UICollectionView) {
        collectionView.register(STOrderNewCollectionViewCell.classForCoder(), forCellWithReuseIdentifier: STOrderNewCollectionViewCell.identifier)
    }
    
    class func reusableCell(_ collectionView: UICollectionView, indexPath: IndexPath) -> STOrderNewCollectionViewCell? {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: STOrderNewCollectionViewCell.identifier, for: indexPath) as? STOrderNewCollectionViewCell
        return cell
    }
    
    weak var delegate: STOrderNewActionDelegate?
    var indexPath: IndexPath = IndexPath.init()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        backgroundColor = UIColor.white
        layer.cornerRadius = 12
        
        addSubview(logisticsView)
        logisticsView.snp.makeConstraints { (make) in
            make.top.equalTo(14)
            make.left.equalTo(10)
            make.right.equalTo(0)
            make.height.equalTo(72)
        }
        
        let bgView: UIView = UIView.init()
        bgView.backgroundColor = UIColor.init(hexString: "#F4F9FF", alpha: 1)
        bgView.layer.cornerRadius = 10
        addSubview(bgView)
        bgView.snp.makeConstraints { (make) in
            make.top.equalTo(logisticsView.snp.bottom).offset(2)
            make.height.equalTo(112)
            make.left.equalTo(16)
            make.right.equalTo(-16)
        }

        bgView.addSubview(yuanView)
        yuanView.snp.makeConstraints { (make) in
            make.top.equalTo(17)
            make.left.equalTo(0)
            make.right.equalTo(0)
            make.height.equalTo(20)
        }

        bgView.addSubview(danView)
        danView.snp.makeConstraints { (make) in
            make.top.equalTo(yuanView.snp.bottom).offset(12)
            make.left.equalTo(0)
            make.right.equalTo(0)
            make.height.equalTo(20)
        }

        bgView.addSubview(shiView)
        shiView.snp.makeConstraints { (make) in
            make.top.equalTo(danView.snp.bottom).offset(12)
            make.left.equalTo(0)
            make.right.equalTo(0)
            make.height.equalTo(20)
        }

        addSubview(returnBtn)
        returnBtn.snp.makeConstraints { (make) in
            make.width.equalTo(128)
            make.height.equalTo(44)
            make.left.equalTo(24)
            make.bottom.equalTo(-12)
        }

        addSubview(waitBtn)
        waitBtn.snp.makeConstraints { (make) in
            make.width.equalTo(128)
            make.height.equalTo(44)
            make.right.equalTo(-24)
            make.bottom.equalTo(-12)
        }
        
        returnBtn.addTarget(self, action: #selector(handleReturnAction), for: .touchUpInside)
        waitBtn.addTarget(self, action: #selector(handlWaitAction), for: .touchUpInside)
    }
    
    // 退还
    @objc func handleReturnAction() {
        delegate?.handleReturnAction(indexPath: self.indexPath)
    }
    
    // 确认接单
    @objc func handlWaitAction() {
        delegate?.handlWaitAction(indexPath: self.indexPath)
    }
    
    public func updateCellData(data: Dictionary<String, Any>, _ indexPath: IndexPath) {
        self.indexPath = indexPath
        yuanView.updateValue(data["driverName"] as! String)
        danView.updateValue(data["orderNo"] as! String)
        shiView.updateValue(data["updateTime"] as! String)
        logisticsView.updateData(data)
        let status = data["status"] as! Int
        returnBtn.isHidden = status != 2
        waitBtn.isHidden = status != 2
    }
    
    lazy var iconView: UIImageView = {
        let imgView = UIImageView.init()
        return imgView
    }()
    
    lazy var logisticsView: STNewOrderLogisticsView = {
        let logView = STNewOrderLogisticsView.init(frame: CGRect.zero)
        return logView
    }()
    
    lazy var yuanView: STNewOrderInfoItemView = {
        let itemView = STNewOrderInfoItemView.init(type: 0)
        return itemView
    }()
   
    lazy var danView: STNewOrderInfoItemView = {
        let itemView = STNewOrderInfoItemView.init(type: 1)
        return itemView
    }()
    
    lazy var shiView: STNewOrderInfoItemView = {
        let itemView = STNewOrderInfoItemView.init(type: 2)
        return itemView
    }()
    
    lazy var returnBtn: UIButton = {
        let btn = UIButton.init(type: .custom)
        btn.setTitle("退还", for: .normal)
        btn.backgroundColor = UIColor.init(hexString: "#E9E9EC", alpha: 1)
        btn.layer.cornerRadius = 22
        btn.setTitleColor(UIColor.init(hexString: "#8F92A1", alpha: 1), for: UIControl.State.normal)
        return btn
    }()
    
    lazy var waitBtn: UIButton = {
        let btn = UIButton.init(type: .custom)
        btn.setTitle("确认接单", for: .normal)
        btn.backgroundColor = UIColor.init(hexString: "#3686FF", alpha: 1)
        btn.layer.cornerRadius = 22
        btn.setTitleColor(UIColor.white, for: UIControl.State.normal)
        return btn
    }()
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
